import csv
csv_path = r'C:\dropbox\My Dropbox\google_appengine\osumapeditor\DATA_STRUCTURE.csv'
f = open(csv_path)

reader = csv.reader(f)
h = reader.next()
FIELDS = []
for l in reader:
    r = dict(zip(h, l))
    FIELDS.append(r)
f.close()


#fields = {'roadway': 'boolean', 'description': 'string', 'end_date': 'date', 'projectno': 'string', 'projectid': 'string', 'project':'string', 'garage':'boolean', 'adaparking': 'boolean', 'sidewalk':'boolean', 'start_date':'date', 'pm':'string'}

dateFormat = 'm/d/Y'


store_tpl = """
var store = new Ext.data.JsonStore({
        root: '%(root)s',
        totalProperty: '%(totalCount)s',
        idProperty: '%(id)s',
        remoteSort: true,

        fields: [
            %(fields)s
        ],

        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: '%(url)s'
        })
    });
    //{'root': root, 'totalCount': 'totalCount', 'id': 'id', 'fields': 'fields', 'url': 'url}

"""

field_tpl = "{name: '%(name)s', type: '%(type)s'}"

date_fld_tpl  = "{name: '%(name)s', type: 'date', dateFormat: 'm/d/Y'}"


results = []
for fld in FIELDS:
    
    if fld['type']=='date':
        t = date_fld_tpl % fld
    else:
        t = field_tpl % fld
    results.append(t)

flds = ',\n'.join(results)

print store_tpl % {'root': 'results', 'totalCount': 'totalCount', 'id': 'id',
                   'fields': flds, 'url': 'http://localhost:8082/test'}
        



grid_tpl = """
    var grid = new Ext.grid.GridPanel({
        width:700,
        height:500,
        title:'%(title)s',
        store: store,
        trackMouseOver:false,
        disableSelection:true,
        loadMask: true,

        // grid columns
        columns:[%(columns)s],

        // customize view config
        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            showPreview:true,
            getRowClass : function(record, rowIndex, p, store){
                if(this.showPreview){
                    p.body = '<p>'+record.data.description+'</p>';
                    return 'x-grid3-row-expanded';
                }
                return 'x-grid3-row-collapsed';
            }
        },

        // paging bar on the bottom
        bbar: new Ext.PagingToolbar({
            pageSize: 25,
            store: store,
            displayInfo: true,
            displayMsg: 'Displaying topics {0} - {1} of {2}',
            emptyMsg: "No topics to display",
            items:[
                '-', {
                pressed: true,
                enableToggle:true,
                text: 'Show Preview',
                cls: 'x-btn-text-icon details',
                toggleHandler: function(btn, pressed){
                    var view = grid.getView();
                    view.showPreview = pressed;
                    view.refresh();
                }
            }]
        })
    });

"""


column_tpl = """
{
            header: "%(header)s",
            dataIndex: '%(name)s',
            width: %(width)s,
            sortable: true
        }"""

results = []

for fld in FIELDS:
    if fld['show']=='Y':
        results.append(column_tpl % fld)

print grid_tpl % {'title': 'Impact List', 'columns': ',\n'.join(results) }
